package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.data.bean.TaobaoOrderBean
import com.app.couponsharelife.databinding.OrderListItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

/**
 * Created by guluwa on 2018/11/22.
 */
class OrderListAdapter(val list: MutableList<Any>,
                       private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: List<Any>, page: Int) {
        if (page == 2) list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.order_list_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            val rowsBean = list[position] as TaobaoOrderBean.RowsBean
            (holder as ViewHolder).databinding.mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(holder.itemView.context)
            holder.databinding.mRecyclerView.adapter = OrderGoodAdapter(arrayListOf(rowsBean))
            holder.databinding.tvOrderStatus.text = when (rowsBean.orderstatus) {
                "0" -> "已失效"
                "1" -> "已付款"
                "2" -> "已结算"
                else -> "已成功"
            }
            holder.databinding.tvOrderNum.text = String.format("订单号 %s", rowsBean.ordersn)
        }
    }

    inner class ViewHolder(val databinding: OrderListItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)
}